package com.lc.luzhengtang.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lc.luzhengtang.BaseApplication;
import com.lc.luzhengtang.R;
import com.lc.luzhengtang.adapter.UnSettledListAdapter;
import com.lc.luzhengtang.base.BaseActivity;
import com.lc.luzhengtang.bean.CommenBean;
import com.lc.luzhengtang.bean.UnsettledBean;
import com.lc.luzhengtang.conn.ConnAlreadyWancheng;
import com.lc.luzhengtang.conn.ConnUnsettled;
import com.lc.luzhengtang.util.LovelyToast;
import com.zcx.helper.http.AsyCallBack;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/6/22.
 */
public class UnsettledActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener {

    @BindView(R.id.img_news)
    ImageView imgNews;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    SwipeRefreshLayout refreshLayout;
    UnSettledListAdapter unSettledListAdapter;
    List<UnsettledBean.DataBean> results;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_unsettled);
        ButterKnife.bind(this);
        results = new ArrayList<>();
        initData();

        unSettledListAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                new ConnAlreadyWancheng(results.get(position).getId() + "", String.valueOf(Double.valueOf(results.get(position).getReward()).intValue()), BaseApplication.basePreferences.readUid(), new AsyCallBack<CommenBean>() {
                    @Override
                    public void onStart(int type) throws Exception {
                        super.onStart(type);
                    }

                    @Override
                    public void onSuccess(String toast, int type, Object o, CommenBean commenBean) throws Exception {
                        super.onSuccess(toast, type, o, commenBean);
                        LovelyToast.makeText(UnsettledActivity.this, commenBean.getMessage(), 0, LovelyToast
                                .SUCCESS, LovelyToast.SCALE);
                        initData();
                    }

                    @Override
                    public void onFail(String toast, int type) throws Exception {
                        super.onFail(toast, type);
                    }

                    @Override
                    public void onEnd(String toast, int type) throws Exception {
                        super.onEnd(toast, type);
                    }
                }


                ).execute(UnsettledActivity.this, false);


            }
        });
    }


    protected void initData() {
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        //recyclerView.setLayoutManager(new LinearLayoutManager(context));
        recyclerView.setLayoutManager(new LinearLayoutManager(UnsettledActivity.this));

        unSettledListAdapter = new UnSettledListAdapter(null);
        unSettledListAdapter.openLoadAnimation(BaseQuickAdapter.SCALEIN);
        unSettledListAdapter.isFirstOnly(false);
    /*    Drawable drawable = ContextCompat.getDrawable(UnsettledActivity.this, R.drawable.line_divider);
        recyclerView.addItemDecoration(new SimpleDividerItemDecoration(UnsettledActivity.this, drawable, 1));*/

        recyclerView.setAdapter(unSettledListAdapter);

        refreshLayout.setColorSchemeColors(Color.RED, Color.BLUE, Color.GREEN);
        refreshLayout.setOnRefreshListener(UnsettledActivity.this);


        //开启loading,获取数据
        setRefreshing(true);
        onRefresh();
    }

    public void unsettledData() {

    }


    @OnClick(R.id.img_news)
    public void onViewClicked() {
        finish();

    }

    @Override
    public void onRefresh() {
        new ConnUnsettled(BaseApplication.basePreferences.readUid(), new AsyCallBack<UnsettledBean>() {
            @Override
            public void onStart(int type) throws Exception {
                super.onStart(type);
            }

            @Override
            public void onSuccess(String toast, int type, Object o, UnsettledBean unsettledBean) throws Exception {
                super.onSuccess(toast, type, o, unsettledBean);

               /* List<ConnCommodity.DataBeanX.DataBean> results = connCommodity.getData().getData();
                if (results != null) {
                    currentPage = 1;
                    classfiyGridAdapter.setNewData(results);
                }*/
                results = unsettledBean.getData();
                unSettledListAdapter.setNewData(results);


            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);
                refreshLayout.setRefreshing(false);
            }
        }


        ).execute(UnsettledActivity.this, false);
    }

    public void setRefreshing(final boolean refreshing) {
        refreshLayout.post(new Runnable() {
            @Override
            public void run() {
                refreshLayout.setRefreshing(refreshing);
            }
        });
    }

    //已完成接口


}
